package com.xc.utils.stock;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author xiongcan
 * @version 1.0
 * @date 2018/10/29 下午4:16
 */
@Slf4j
public class BuyAndSellUtils {

    /**
     *  是否在交易时间内
     * @param begin_time    HH:mm
     * @param end_time      HH:mm
     * @return
     */
    public static boolean isTransTime(String begin_time,String end_time) throws Exception{
        if(StringUtils.isBlank(begin_time) || StringUtils.isBlank(end_time)){
            return false;
        }
        Date nowDate = new Date();
        if(!isWorkDay(nowDate)){
            return false;
        }
        SimpleDateFormat df = new SimpleDateFormat("HH:mm");
        Date now = null;
        Date beginTime = null;
        Date endTime = null;
        try {
            now = df.parse(df.format(nowDate));
            beginTime = df.parse(begin_time);
            endTime = df.parse(end_time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Boolean flag = belongCalendar(now, beginTime, endTime);
//        System.out.println(flag);
        return flag;
    }

    public static boolean belongCalendar (Date nowTime, Date beginTime, Date endTime){
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    //  判断当前时间是否是工作日（开盘日）
    public static boolean isWorkDay(Date currentDate)throws Exception{
//        DateFormat format1 = new SimpleDateFormat("yyyy/MM/dd");
//        Date bdate = format1.parse("2019/01/18");
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentDate);
        if(cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY
                || cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY){
            return false;
        } else{
            return true;
        }
    }



    public static void main(String[] args) throws Exception{
        System.out.println(isTransTime("16:30","16:31"));
    }




}



